package net.jarang.study.spring.web;

import java.util.Properties;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jarang.study.spring.annotation.User;
import net.jarang.study.spring.board.VisitBO;
import net.jarang.study.spring.model.PrototypeScopeBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.RedirectView;


@Controller
public class ExampleController {	
	//	@Autowired private Shape shape;
	
	@Autowired private ApplicationContext context;
	@Autowired private ResourceLoader resourceLoader;
	@Resource private Properties systemProperties;
	
	@Autowired PrototypeScopeBean prototypeScopBean1;
	@Autowired PrototypeScopeBean prototypeScopBean2;
	
//	@Autowired BoardBO boardBO;
//	@Autowired CommentBO commentBO;
	@Autowired VisitBO visitBO;
	
	@RequestMapping("/redirect1")
	public ModelAndView redirect1() {
		ModelAndView mav = new ModelAndView("redirect:http://naver.com");
		return mav;
	}
	
	@RequestMapping("/redirect2")
	public ModelAndView redirect2() {
		ModelAndView mav = new ModelAndView("redirect:request1.nhn");
		return mav;
	}

	@RequestMapping("/redirect3")
	public ModelAndView redirect3() {
		ModelAndView mav = new ModelAndView(new RedirectView("request1.nhn"));
		return mav;
	}
	
	@RequestMapping("/interceptor/test")
	public ModelAndView interceptor(String userName) {
		ModelAndView mav = new ModelAndView("result");
		mav.addObject("result1", userName);
		return mav;
	}	
	
	@RequestMapping("/request1")
	public ModelAndView request1(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView(new InternalResourceView("/WEB-INF/jsp/result.jsp"));
		mav.addObject("result1", request);
		mav.addObject("result2", response);
		return mav;
	}
	
	@RequestMapping("/request2")
	public ModelAndView request2(User user) {
		ModelAndView mav = new ModelAndView("result");
		mav.addObject("result1", user.getId());
		mav.addObject("result2", user.getName());
		return mav;
	}
	
	@RequestMapping("/request3")
	public String request3() {
		return "result";
	}
	
	@RequestMapping("/error")
	public ModelAndView error() throws Exception {
		throw new Exception("process error!!");
	}	
	
	@RequestMapping("/autobean")
	public ModelAndView autoBean() {
		ModelAndView mav = new ModelAndView("result");
		mav.addObject("result1", context.getBean("color"));
		mav.addObject("result2", resourceLoader.getResource("classpath:info.data").exists());
		mav.addObject("result3", systemProperties.get("os.name"));
		return mav;
	}
	
	@RequestMapping("/prototype")
	public ModelAndView prototype() {
		ModelAndView mav = new ModelAndView("result");
		mav.addObject("result1", prototypeScopBean1);
		mav.addObject("result2", prototypeScopBean2);
		return mav;
	}	
	
//	@RequestMapping("/draw")
//	public ModelAndView mainPage() {
//		String draw = shape.draw();
//		ModelAndView mav = new ModelAndView("result");
//		mav.addObject("result1", draw);
//		return mav;
//	}
//	
//	@RequestMapping("/boardName")
//	public ModelAndView boardName() {
//		String resutl1 = boardBO.getBoardName();
//		ModelAndView mav = new ModelAndView("result");
//		mav.addObject("result1", resutl1);
//		return mav;
//	}
//	
//	@RequestMapping("/comment")
//	public ModelAndView commentName() {
//		String resutl1 = commentBO.getName();
//		int resutl2 = commentBO.getSize();
//		ModelAndView mav = new ModelAndView("result");
//		mav.addObject("result1", resutl1);
//		mav.addObject("result2", resutl2);
//		return mav;
//	}
//
//	@RequestMapping("/write")
//	public ModelAndView write() {
//		String resutl1 = boardBO.write();
//		ModelAndView mav = new ModelAndView("result");
//		mav.addObject("result1", resutl1);
//		return mav;
//	}

}
